package com.zhengb.algorithmdemo.basic.tree;

import com.zhengb.algorithmdemo.basic.queue.LinkListBaseQueue;
import com.zhengb.algorithmdemo.basic.stack.LinkListBaseStack;

import java.util.*;

/**
 * 二叉树遍历
 *
 * @author zhengbo
 * @date 2020/2/19
 */
public class BinaryTreeTraverse {

    /**
     * 递归方式前序遍历二叉树
     * 时间复杂度O(N),空间复杂度 O(N)
     *
     * @param rootNode
     * @param <T>
     */
    public static <T> void preOrderTraverse(BinaryTreeNode<T> rootNode) {

        if (rootNode != null) {
            System.out.println(rootNode.getData());
            preOrderTraverse(rootNode.getLeftNode());
            preOrderTraverse(rootNode.getRightNode());
        }
    }

    /**
     * 中序遍历
     * 时间复杂度O(N),空间复杂度 O(N)
     *
     * @param rootNode
     * @param <T>
     */
    public static <T> void inOderTraverse(BinaryTreeNode<T> rootNode) {

        if (rootNode != null) {
            inOderTraverse(rootNode.getLeftNode());
            System.out.println(rootNode.getData());
            inOderTraverse(rootNode.getRightNode());
        }
    }

    /**
     * 后序遍历
     * 时间复杂度O(N),空间复杂度 O(N)
     *
     * @param rootNode
     * @param <T>
     */
    public static <T> void postOderTraverse(BinaryTreeNode<T> rootNode) {

        if (rootNode != null) {
            postOderTraverse(rootNode.getLeftNode());
            postOderTraverse(rootNode.getRightNode());
            System.out.println(rootNode.getData());
        }
    }

    /**
     * 非递归方式前序遍历二叉树
     * 时间复杂度O(N),空间复杂度 O(N)
     *
     * @param rootNode
     * @param <T>
     */
    public static <T> void preOrderNoRecursive(BinaryTreeNode<T> rootNode) {

        if (rootNode != null) {
            LinkListBaseStack<BinaryTreeNode<T>> stack = new LinkListBaseStack<>();

            while (true) {
                while (rootNode != null) {
                    System.out.println(rootNode.getData());
                    stack.push(rootNode);
                    rootNode = rootNode.getLeftNode();
                }
                if (stack.isEmpty()) {
                    return;
                }

                rootNode = stack.pop();

                rootNode = rootNode.getRightNode();
            }
        }
    }

    /**
     * 非递归前序遍历二叉树
     * 使用jdk stack
     *
     * @param rootNode
     * @param <T>
     * @return
     */
    public static <T> List<T> traverseTreeWithPreOrder(BinaryTreeNode<T> rootNode) {

        List<T> resultList = new ArrayList<>();

        Stack<BinaryTreeNode<T>> stack = new Stack<>();

        BinaryTreeNode<T> tempNode = rootNode;

        while (true) {
            while (tempNode != null) {
                //先处理根节点
                resultList.add(tempNode.getData());
                //处理完后入栈
                stack.push(tempNode);
                //遍历做左节点
                tempNode = tempNode.getLeftNode();
            }

            if (stack.isEmpty()) {
                break;
            }
            //遍历完左节点之后 将根节点弹出
            tempNode = stack.pop();
            //遍历右节点
            tempNode = tempNode.getRightNode();
        }

        return resultList;
    }

    /**
     * 非递归中序遍历二叉树
     * 使用jdk stack
     *
     * @param rootNode
     * @param <T>
     * @return
     */
    public static <T> List<T> traverseTreeWithInOrder(BinaryTreeNode<T> rootNode) {

        List<T> resultList = new ArrayList<>();

        Stack<BinaryTreeNode<T>> stack = new Stack<>();

        BinaryTreeNode<T> tempNode = rootNode;

        while (true) {
            while (tempNode != null) {
                stack.push(tempNode);
                tempNode = tempNode.getLeftNode();
            }

            if (stack.isEmpty()) {
                break;
            }

            tempNode = stack.pop();
            resultList.add(tempNode.getData());

            tempNode = tempNode.getRightNode();
        }

        return resultList;
    }

    public static <T> List<T> traverseTreeWithPostOrder(BinaryTreeNode<T> rootNode) {

        Stack<BinaryTreeNode<T>> stack = new Stack<>();

        List<T> resultList = new LinkedList<>();

        BinaryTreeNode<T> tempNode = rootNode;

        while (true) {
            if (tempNode != null) {
                stack.push(tempNode);
                tempNode = tempNode.getLeftNode();
            } else {
                if (stack.isEmpty()) {
                    return resultList;
                } else if (stack.peek().getRightNode() == null) {
                    tempNode = stack.pop();

                    resultList.add(tempNode.getData());

                    if (!stack.isEmpty() && tempNode == stack.peek().getRightNode()) {
                        resultList.add(stack.peek().getData());
                        stack.pop();
                    }
                }
                if (!stack.isEmpty()) {
                    tempNode = stack.pop().getRightNode();
                } else {
                    tempNode = null;
                }
            }
        }
//        return resultList;
    }


    public static <T> void levelOrder(BinaryTreeNode<T> rootNode) {

        BinaryTreeNode<T> temp;

        LinkListBaseQueue<BinaryTreeNode<T>> queue = LinkListBaseQueue.createQueue();

        queue.enQueue(rootNode);

        while (!queue.isEmpty()) {
            temp = queue.deQueue();
            System.out.println(temp.getData());

            if (temp.getLeftNode() != null) {
                queue.enQueue(temp.getLeftNode());
            }
            if (temp.getRightNode() != null) {
                queue.enQueue(temp.getRightNode());
            }
        }

    }

    public static void main(String[] args) {


    }
}
